package demo.controller;

import demo.Constants.PermissionConstant;
import demo.annotation.RequiredPermissions;
import demo.entity.employees;
import demo.entity.managers;
import demo.entity.record;
import demo.entity.users;
import demo.service.CommonService;
import demo.service.TopService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;



import java.util.List;
@CrossOrigin
@RestController
@RequestMapping(path="/top")
public class TopController {
    private Logger logger = Logger.getLogger(TopController.class);

    @Autowired
    private TopService topService;
    private CommonService commonService;

    public TopController(CommonService commonService) {
        this.commonService = commonService;
    }

    @PostMapping("/addUsers")
    @RequiredPermissions(PermissionConstant.TOP)
    users addUsers(@RequestBody users user) {
        int emp_id = user.getEmp_id();
        logger.info("：新增工号为 " + emp_id + " 的用户");
        return topService.addUsers(user);
    }

    @PostMapping("/addEmployees")
    @RequiredPermissions(PermissionConstant.TOP)
    employees addEmployees(@RequestBody employees emp) {
        int emp_id = emp.getEmp_id();
        logger.info("：新增工号为 " + emp_id + " 的员工");
        return topService.addEmployees(emp);
    }

    @DeleteMapping("/dropEmployees")
    @RequiredPermissions(PermissionConstant.TOP)
    void dropEmployees(@RequestParam(value = "emp_id")int emp_id) {
        logger.info("：删除工号为 " + emp_id + " 的员工");
        topService.dropEmployeesById(emp_id);
    }
    @GetMapping("/queryAllEmployees")
    @RequiredPermissions(PermissionConstant.TOP)
    List<employees> queryAllEmployees() {
        logger.info("：查询所有员工的信息");
        return topService.queryAllEmployees();
    }
    @GetMapping("/queryEmployees")
    @RequiredPermissions(PermissionConstant.TOP)
    employees queryEmployees(@RequestParam(value = "emp_id")int emp_id) {
        logger.info("：查询工号为 " + emp_id + " 的员工信息");
        return topService.queryEmployeesById(emp_id);
    }
    @PutMapping("/changeRestHoliday")
    @RequiredPermissions(PermissionConstant.TOP)
    employees changeRestHoliday(@RequestParam(value = "emp_id")int emp_id ,@RequestParam(value = "newValue")int newRestHoliday ){
        logger.info(": 更改工号为 " + emp_id + " 的员工的假期为 " + newRestHoliday);
        return  topService.changeRestHoliday(emp_id,newRestHoliday);
    }
    @PutMapping("/changeBasic_Salary")
    @RequiredPermissions(PermissionConstant.TOP)
    employees changeBasic_Salary(@RequestParam(value = "emp_id")int emp_id ,@RequestParam(value = "newValue")int newBasic_Salary ){
        logger.info(": 更改工号为 " + emp_id + " 的员工的基础工资为 " + newBasic_Salary);
        return  topService.changeBasic_Salary(emp_id,newBasic_Salary);
    }
    @PutMapping("/changeMonthly_Salary")
    @RequiredPermissions(PermissionConstant.TOP)
    employees changeMonthly_Salary(@RequestParam(value = "emp_id")int emp_id ,@RequestParam(value = "newValue")int newMonthly_Salary ){
        logger.info(": 更改工号为 " + emp_id + " 的员工的当月工资为 " + newMonthly_Salary);
        return  topService.changeMonthly_Salary(emp_id,newMonthly_Salary);
    }
    @PutMapping("/changeArrival_time")
    @RequiredPermissions(PermissionConstant.TOP)
    employees changeArrival_time(@RequestParam(value = "emp_id")int emp_id ,@RequestParam(value = "newValue")String newArrival_time ){
        logger.info(": 更改工号为 " + emp_id + " 的员工的应到岗时间为 " + newArrival_time);
        return  topService.changeArrival_time(emp_id,newArrival_time);
    }
    @PutMapping("/changeDeparture_time")
    @RequiredPermissions(PermissionConstant.TOP)
    employees changeDeparture_time(@RequestParam(value = "emp_id")int emp_id ,@RequestParam(value = "newValue")String newDeparture_time ){
        logger.info(": 更改工号为 " + emp_id + " 的员工的应离岗时间为 " + newDeparture_time);
        return  topService.changeDeparture_time(emp_id,newDeparture_time);
    }
    @PutMapping("/changeDepartment_id")
    @RequiredPermissions(PermissionConstant.TOP)
    employees changeDepartment_id(@RequestParam(value = "emp_id")int emp_id ,@RequestParam(value = "newValue")String newDepartment_id ){
        logger.info(": 更改工号为 " + emp_id + " 的员工的所在部门为 " + newDepartment_id);
        return  topService.changeDepartment_id(emp_id,newDepartment_id);
    }
    @PutMapping("/changeJob_id")
    @RequiredPermissions(PermissionConstant.TOP)
    employees changeJob_id(@RequestParam(value = "emp_id")int emp_id ,@RequestParam(value = "newValue")String newJob_id ){
        logger.info(": 更改工号为 " + emp_id + " 的员工职位为 " + newJob_id);
        return  topService.changeJob_id(emp_id,newJob_id);
    }
    @GetMapping("/queryRecord") //查询某人打卡记录
    @RequiredPermissions(PermissionConstant.TOP)
    List<record> queryRecord(@RequestParam(value = "emp_id")int emp_id) {
        logger.info("：查询工号为 " + emp_id + " 的员工的打卡记录");
        return commonService.queryRecord(emp_id);
    }
    @GetMapping("/queryAllRecord") //查询所有人打卡记录
    @RequiredPermissions(PermissionConstant.TOP)
    List<record> queryAllRecord() {
        logger.info("：查询所有人打卡记录");
        return topService.queryAllRecord();
    }
    @GetMapping("/queryManagers")
    @RequiredPermissions(PermissionConstant.TOP)
    List<managers> queryManagers() {
        logger.info("：查询所有请假申请");
        return topService.queryManagers();
    }
    @GetMapping("/queryManagersById")
    @RequiredPermissions(PermissionConstant.TOP)
    managers queryManagersById(@RequestParam(value = "id")int id) {
        logger.info("：查询id为 " + id + " 的请假申请");
        return topService.queryManagersById(id);
    }
    @PutMapping("/dealManagers")
    @RequiredPermissions(PermissionConstant.TOP)
    managers dealManagers(@RequestParam(value = "id")int id ,@RequestParam(value = "status")String status ){
        logger.info("：批复id为 " + id + " 的请假申请并且批复结果为 " + status);
        return  topService.dealManagers(id,status);
    }

}
